有没有办法在用户界面的其他地方显示textInput()
的值,而无需通过server.R
处理如下所示的非常详细的内容?
ui.R
library(shiny)
shinyUI(
fluidPage(
textInput('text_in', label = 'Write text here'),
# elsewhere in the UI...
textOutput('text_out')
))
server.R
library(shiny)
shinyServer(function(input, output) {
output$text_out = renderText(input$text_in)
})
对于这个例子来说并不是太糟糕,但是当我需要多次这样做时它会变得非常冗长。我的愿望是收集用户在整个应用程序中输入的所有输入,并在最后将它们编译成一个漂亮的表,这样他们就可以确认所有内容都是正确的。
我发现在conditionalPanel()
中使用JavaScript表达式时,您可以在不通过服务器的情况下引用输入元素,但我不确定如何在此特定实例之外实现它。
答案 0 :(得分:11)
For accessing all inputs, you can use reactiveValuesToList
server-side. You can access input values via Javascript Events like below (I have taken the example from @Pork Chop) :
library(shiny)
ui <- basicPage(
fluidRow(
column(
width = 6,
textInput('a', 'Text A',"a1"),
textInput('b', 'Text B',"b1"),
textInput('c', 'Text A',"c1"),
textInput('d', 'Text B',"d1"),
textInput('e', 'Text A',"e1"),
textInput('f', 'Text B',"f1")
),
column(
width = 6,
tags$p("Text A :", tags$span(id = "valueA", "")),
tags$script(
"$(document).on('shiny:inputchanged', function(event) {
if (event.name === 'a') {
$('#valueA').text(event.value);
}
});
"
),
tableOutput('show_inputs')
)
)
)
server <- shinyServer(function(input, output, session){
AllInputs <- reactive({
x <- reactiveValuesToList(input)
data.frame(
names = names(x),
values = unlist(x, use.names = FALSE)
)
})
output$show_inputs <- renderTable({
AllInputs()
})
})
shinyApp(ui = ui, server = server)
答案 1 :(得分:3)
由于您的总体目标是收集所有用户输入,然后将它们编译成表格,我将通过下面的示例向您展示如何实现这一目标。如您所见,所有input
变量都可以通过server
中的名称访问。我让他们处于被动状态,以防您需要进行进一步分析或使用renderUI
功能。
#rm(list=ls())
library(shiny)
ui <- basicPage(
textInput('a', 'Text A',"a1"),
textInput('b', 'Text B',"b1"),
textInput('c', 'Text A',"c1"),
textInput('d', 'Text B',"d1"),
textInput('e', 'Text A',"e1"),
textInput('f', 'Text B',"f1"),
tableOutput('show_inputs')
)
server <- shinyServer(function(input, output, session){
AllInputs <- reactive({
myvalues <- NULL
for(i in 1:length(names(input))){
myvalues <- as.data.frame(rbind(myvalues,(cbind(names(input)[i],input[[names(input)[i]]]))))
}
names(myvalues) <- c("User Input","Last Value")
myvalues
})
output$show_inputs <- renderTable({
AllInputs()
})
})
shinyApp(ui = ui, server = server)
答案 2 :(得分:1)
如果“闪闪发光”输入的所有长度都不同,并且以上命令均无效(例如,如果您具有单选按钮,复选框组,textInput等的组合),那么它将起作用,并且可以产生一个variable:value
表可以稍后解析:
AllInputs <- reactive({
myvalues <- NULL
newvalues <- NULL
for(i in 1:length(names(input))){
newvalues <- paste(names(input)[i], input[[names(input)[i]]], sep=":")
myvalues <- append(myvalues, newvalues)
}
myvalues
})
output$show_inputs <- renderTable({
AllInputs()
})
答案 3 :(得分:0)
同时借鉴 @CommonsLog
public class CustomGatewayFilterFactory extends AbstractGatewayFilterFactory< CustomGatewayFilterFactory.Config> {
protected static final ObjectMapper MAPPER = new ObjectMapper();
public CustomGatewayFilterFactory() {
super(Config.class);
}
@Override
public GatewayFilter apply(Config config) {
return new OrderedGatewayFilter((exchange, chain) -> {
return chain.filter(exchange.mutate().request(request).build()).s
then(
Mono.fromRunnable(() -> {
ServerHttpResponse response = exchange.getResponse();
//response.getBody() //Dont know how to read and modify the body
Optional.ofNullable(exchange.getRequest()
.getQueryParams()
.getFirst("include-total"))
.ifPresent(qp -> {
String responseContentLanguage = "hai";
response.getHeaders()
.add("Bael-Custom-Language-Header", responseContentLanguage);
});
}));
}, 10);
}
public static class Config {
}
}
和 Pork Chop
,这是一个适用于闪亮的多种类型文本和数字输入的解决方案。
mysteRious
注意:library(shiny)
AdvRchp3 <- "While you’ve probably already used many (if not all)
of the different types of vectors, you may not have thought
deeply about how they’re interrelated. In this chapter,
I won’t cover individual vectors types in too much detail,
but I will show you how all the types fit together as a whole.
If you need more details, you can find them in R’s documentation."
ui <- fluidPage(
fluidRow(
h4("Text Inputs"),
textInput("text_input", "Input some Text", value = "some text"),
passwordInput("password_input", "Input a Password", value = "1234"),
textAreaInput("text_area_input", "Input lots of Text", rows = 3, value = AdvRchp3)
),
fluidRow(
h4("Numeric Inputs"),
numericInput("numeric_input", "Number 1", value = 1, min = 0, max = 100),
sliderInput("slider_input_single", "Number 50", value = 50, min = 0, max = 100),
sliderInput("slider_input_ranges", "Range 10 to 20", value = c(10, 20), min = 0, max = 100)
),
fluidRow(
tableOutput("show_inputs")
)
)
server <- function(input, output, session) {
all_inputs <- reactive({
input_df <- NULL
df_row <- NULL
for(i in 1:length(names(input))){
df_row <- as.data.frame(cbind(names(input)[i], input[[names(input)[i]]]))
input_df <- as.data.frame(dplyr::bind_rows(input_df, df_row))
}
names(input_df) <- c("input_id", "input_val")
input_df
})
output$show_inputs <- renderTable({
all_inputs()
})
}
shinyApp(ui, server)
现在是 rbind
,但 dplyr::bind_rows
也可以使用。