在 R Shiny 中,使用 textOutput 动态填充下载按钮的标签
In R Shiny, use textOutput to dynamically populate downloadbutton's label
在 R Shiny 中,我尝试使用反应式 renderText 和 textoutput 动态设置下载按钮的标签。
它按预期工作,但标签始终显示在新行中,因此该按钮在常规按钮旁边看起来很古怪
as shown here
后端逻辑是-
在 server.R 中,输入字段的值用于生成条件标签
output$mycustomlabel <- renderText({ if(input$inputtype=="One") return("Download label 1") else return("Download label 2")})
然后在UI.R中,该标签用作
downloadButton("download.button.test", textOutput("mycustomlabel"))
谁能指导为什么它在新行显示文本,我怎样才能让它保持在同一行?
如果您想更改按钮标签,您可能需要使用 javascript 更新它。
一种更简单的方法是使用两个不同的按钮并使用条件面板显示其中一个按钮:
ui <- fluidPage(
radioButtons('inputtype', 'Set label', c('One', 'Two')),
conditionalPanel(
'input.inputtype == "One"',
downloadButton('btn1', 'Download label 1')
),
conditionalPanel(
'input.inputtype == "Two"',
downloadButton('btn2', 'Download label 2')
)
)
请注意,使用这种方法,您确实需要在服务器功能中使用两个观察者。
我在 downloadButton
上用动态标签做同样的事情。就我而言,我希望用户选择将数据帧下载为 Excel 文件还是 CSV 文件。
这是我正在做的事情:
在 ui 定义中,您希望按钮显示的位置使用
uiOutput( 'myCustomButtonUI' )
在服务器定义中,包括:
output$myCustomButtonUI <- renderUI({
myCustomLabel <- 'Placeholder'
if( input$inputtype == 'One' ) myCustomLabel <- 'Download Label 1'
if( input$inputtype == 'Two' ) myCustomLabel <- 'Download Label 2'
downloadButton( inputId = 'download.button.test',
label = myCustomLabel )
})
output$download.button.text <- downloadHandler(
filename = "<some filename>",
content = .... <go look up downloadHandler() if you're unfamiliar> ..."
)
想法是,因为您希望按钮是动态的,所以它需要在服务器端呈现。服务器端的输出是 UI 的一小部分,由 uiOutput
函数放置在较大的 UI 中。
在 R Shiny 中,我尝试使用反应式 renderText 和 textoutput 动态设置下载按钮的标签。 它按预期工作,但标签始终显示在新行中,因此该按钮在常规按钮旁边看起来很古怪 as shown here
后端逻辑是-
在 server.R 中,输入字段的值用于生成条件标签
output$mycustomlabel <- renderText({ if(input$inputtype=="One") return("Download label 1") else return("Download label 2")})
然后在UI.R中,该标签用作
downloadButton("download.button.test", textOutput("mycustomlabel"))
谁能指导为什么它在新行显示文本,我怎样才能让它保持在同一行?
如果您想更改按钮标签,您可能需要使用 javascript 更新它。
一种更简单的方法是使用两个不同的按钮并使用条件面板显示其中一个按钮:
ui <- fluidPage(
radioButtons('inputtype', 'Set label', c('One', 'Two')),
conditionalPanel(
'input.inputtype == "One"',
downloadButton('btn1', 'Download label 1')
),
conditionalPanel(
'input.inputtype == "Two"',
downloadButton('btn2', 'Download label 2')
)
)
请注意,使用这种方法,您确实需要在服务器功能中使用两个观察者。
我在 downloadButton
上用动态标签做同样的事情。就我而言,我希望用户选择将数据帧下载为 Excel 文件还是 CSV 文件。
这是我正在做的事情:
在 ui 定义中,您希望按钮显示的位置使用
uiOutput( 'myCustomButtonUI' )
在服务器定义中,包括:
output$myCustomButtonUI <- renderUI({
myCustomLabel <- 'Placeholder'
if( input$inputtype == 'One' ) myCustomLabel <- 'Download Label 1'
if( input$inputtype == 'Two' ) myCustomLabel <- 'Download Label 2'
downloadButton( inputId = 'download.button.test',
label = myCustomLabel )
})
output$download.button.text <- downloadHandler(
filename = "<some filename>",
content = .... <go look up downloadHandler() if you're unfamiliar> ..."
)
想法是,因为您希望按钮是动态的,所以它需要在服务器端呈现。服务器端的输出是 UI 的一小部分,由 uiOutput
函数放置在较大的 UI 中。