我们允许用户更新3个输入中的任何一个。 Shiny计算其中的比率并以%显示。但是,此显示的值可以闪亮显示。
由于显示的值对于代码中的进一步计算是必需的,因此,此行为是不希望的。
因此,有人可以帮助显示以%表示的值,但不允许用户对其进行编辑。
请在下面找到reprex代码。试过了 1.呈现为打印和冲刺,但很难格式化 2.禁用了renderUI,但是代码没有运行,因为renderUI没有运行。
library(shiny)
ui <- fluidPage(
column(6,
tags$h2("Allow the user to change only here"),
numericInput("valueA", "Value1", value = .333, min = 0, max = 1, step = .1),
numericInput("valueB", "Value2", value = .333, min = 0, max = 1, step = .1),
numericInput("valueC", "Value3", value = .333, min = 0, max = 1, step = .1),
verbatimTextOutput("result")
),
column(6,
uiOutput("ui")
)
)
server <- function(input, output, session) {
output$ui <- renderUI( {
tagList(
tags$h2("Display in % but dont allow user to change here"),
numericInput("obs1", "Label1", value = 100 * (input$valueA / (input$valueA + input$valueB + input$valueC))),
numericInput("obs2", "Label2", value = 100 * (input$valueB / (input$valueA + input$valueB + input$valueC))),
numericInput("obs3", "Label3", value = 100 * (input$valueC / (input$valueA + input$valueB + input$valueC)))
)
})
# Since the below option is hard to render like above
# output$result <- renderPrint({
# print(sprintf("A=%.3f, B = %.3f",
# input$obs1,input$obs2))
# })
#### Code to use the values from obs1,obs2,obs3....
}
shinyApp(ui, server)
基本上,应该显示3个用户可以编辑的值和比率(%)。 但是这些百分比不可修改。
答案 0 :(得分:0)
我希望将其打印为verbatimeTextOutput,但这在renderUi中是不可能的(据我所知)。 renderText可在renderUI中使用,但必须保证输出效果不是那么好。我不明白什么?如果您需要0-1的值,为什么不允许输入百分比并重新计算。
library(shiny)
ui <- fluidPage(
column(6,
tags$h2("Allow the user to change only here"),
numericInput("valueA", "Value1", value = .333, min = 0, max = 1, step = .1),
numericInput("valueB", "Value2", value = .333, min = 0, max = 1, step = .1),
numericInput("valueC", "Value3", value = .333, min = 0, max = 1, step = .1),
verbatimTextOutput("result")
),
column(6,
uiOutput("ui")
)
)
server <- function(input, output, session) {
output$ui <- renderUI( {
tagList(
tags$h2("Display in % but dont allow user to change here"),
renderText(paste(100 * (input$valueA / (input$valueA + input$valueB + input$valueC)))),
renderText(paste(value = 100 * (input$valueB / (input$valueA + input$valueB + input$valueC)))),
renderText(paste(value = 100 * (input$valueC / (input$valueA + input$valueB + input$valueC))))
)
})
# Since the below option is hard to render like above
output$result <- renderText({
paste(100 * (input$valueA / (input$valueA + input$valueB + input$valueC)),'%')
})
#### Code to use the values from obs1,obs2,obs3....
}
shinyApp(ui, server)
答案 1 :(得分:0)
好吧,我想我知道您想做什么。您需要将值存储在响应式“变量”或响应式值中(两者都可以侦听输入更改)。以下示例既显示了选项,也显示了以后可以使用的选项。
library(shiny)
ui <- fluidPage(
column(6,
tags$h2("Allow the user to change only here"),
numericInput("valueA", "Value1", value = .333, min = 0, max = 1, step = .1),
numericInput("valueB", "Value2", value = .333, min = 0, max = 1, step = .1),
numericInput("valueC", "Value3", value = .333, min = 0, max = 1, step = .1),
verbatimTextOutput("result")
),
column(6,
uiOutput("ui1"),
tags$hr(),
uiOutput("ui2"),
tags$hr(),
tags$h3("Obs1 * 2"),
verbatimTextOutput("obs1")
)
)
server <- function(input, output, session) {
### reactive "variables"
obs1 <- reactive({
as.numeric(100 * (input$valueA / (input$valueA + input$valueB + input$valueC)))
})
obs2 <- reactive({
as.numeric(100 * (input$valueB / (input$valueA + input$valueB + input$valueC)))
})
obs3 <- reactive({
as.numeric(100 * (input$valueC / (input$valueA + input$valueB + input$valueC)))
})
### or as reative values which I like more
# create
obs <- reactiveValues(obs1 = NULL,
obs2 = NULL,
obs3 = NULL)
# listen to changes
observe({
obs$obs1 <- as.numeric(100 * (input$valueA / (input$valueA + input$valueB + input$valueC)))
obs$obs2 <- as.numeric(100 * (input$valueB / (input$valueA + input$valueB + input$valueC)))
obs$obs3 <- as.numeric(100 * (input$valueC / (input$valueA + input$valueB + input$valueC)))
})
### render ui of reactives variable
# ! note the function notation of obs1() to obs3()
output$ui1 <- renderUI( {
tagList(
tags$h3("Display in % but dont allow user to change here"),
renderText( obs1() ),
renderText( obs2() ),
renderText( obs3() )
)
})
### render ui of reactive values
# ! note: variable called as a normal list
output$ui2 <- renderUI( {
tagList(
tags$h3("Display in % but dont allow user to change here"),
renderText( obs$obs1 ),
renderText( obs$obs2 ),
renderText( obs$obs3 )
)
})
### Code to use the values from obs1,obs2,obs3....
output$obs1<- renderText(obs$obs1 * 2)
}
shinyApp(ui, server)