R 使用闪亮的应用程序动态可视化字符串替换的每个迭代?
我想修改一些R代码的输入并以更具交互性的方式显示其输出。我认为这对于一个闪亮的应用程序来说是一个理想的任务,但我对编写它们不是很熟悉。我有一些R代码,它接受一个文本字符串,并通过在随机位置添加字母或单词进行迭代更改:R 使用闪亮的应用程序动态可视化字符串替换的每个迭代?,r,string,shiny,shiny-reactivity,R,String,Shiny,Shiny Reactivity,我想修改一些R代码的输入并以更具交互性的方式显示其输出。我认为这对于一个闪亮的应用程序来说是一个理想的任务,但我对编写它们不是很熟悉。我有一些R代码,它接受一个文本字符串,并通过在随机位置添加字母或单词进行迭代更改: library(tidyverse) evolve_sentence <- function(sentence, arg2) { chars <- str_split(sentence, "") %>% pluck(1) if (runif(1) >
library(tidyverse)
evolve_sentence <- function(sentence, arg2) {
chars <- str_split(sentence, "") %>% pluck(1)
if (runif(1) > 0.5) {
chars[sample(1:length(chars), 1)] <- sample(chars, 1)
}
sentence <- str_c(chars, collapse = "")
words <- str_split(sentence, " ") %>% pluck(1)
if (runif(1) > 0.9) {
words[sample(1:length(words), 1)] <- sample(words, 1)
}
sentence <- str_c(words, collapse = " ")
sentence
}
tbl_evolve <- tibble(iteration = 1:500, text = "I met a traveller from an antique land")
for (i in 2:500) {
tbl_evolve$text[i] <- evolve_sentence(tbl_evolve$text[i - 1])
}
tbl_evolve %>%
distinct(text, .keep_all = TRUE)
我很乐意将其作为一个闪亮的应用程序展示,用户可以指定输入文本和不同类型更改的概率。对于后者,用户可以指定(runif(1)>0.5)和(runif(1)>0.9)中的值。我知道这在使用insert UI和actionButton
时是可能的
我不太确定是否有一种方法可以动态显示输出,这样用户就可以直观地看到代码的每个迭代(每个迭代之间有一个定义的时间延迟?),而不是像现有代码一样一次看到所有迭代的输出。我对动态可视化输出的不同方式持开放态度,但我认为理想情况下,用户会看到每个迭代都被下一个迭代所取代,并有一个时间延迟。
我还希望有一个带有当前输出的选项卡,每个迭代都是一行,这样用户就可以返回并查看每个迭代
如果您能告诉我这在Shiny中是否可行,或者我是否需要其他工具,我们将不胜感激。库(Shiny)
library(shiny)
library(tidyverse)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Simple Testcase"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
textInput("textinput", "Type text here"),
numericInput("p1", "Probability1", value = 0.5),
numericInput("p2", "Probability2", value = 0.9),
sliderInput("iteration", "Iterations", min = 20, max = 1000, step = 10, value = 100),
actionButton("calc", "Run Calculation!")
),
# Show a plot of the generated distribution
mainPanel(
tableOutput("ui")
)
)
)
# Define server logic required to draw a histogram
server <- function(session ,input, output) {
vals <- reactiveValues(counter = 0)
result <- eventReactive(input$calc, {
evolve_sentence <- function(sentence, arg2) {
chars <- str_split(sentence, "") %>% pluck(1)
if (runif(1) > input$p1) { # Value from numericinput p2
chars[sample(1:length(chars), 1)] <- sample(chars, 1)
}
sentence <- str_c(chars, collapse = "")
words <- str_split(sentence, " ") %>% pluck(1)
if (runif(1) > input$p2) { # Value from numericinput p2
words[sample(1:length(words), 1)] <- sample(words, 1)
}
sentence <- str_c(words, collapse = " ")
sentence
}
tbl_evolve <- tibble(iteration = 1:500, text = input$textinput)
for (i in 2:500) {
tbl_evolve$text[i] <- evolve_sentence(tbl_evolve$text[i - 1])
}
output <-tbl_evolve %>%
distinct(text, .keep_all = TRUE)
print(output)
output
})
output$ui <- renderTable({
df <- result()
invalidateLater(millis = 300, session)
vals$counter <- isolate(vals$counter) + 1
while(nrow(df) < vals$counter) {
vals$counter <- isolate(vals$counter) + 1
} #Prevent to add infinite empty columns.
for(i in 1:nrow(df)) {
newdf <- df[1:vals$counter,]
}
newdf
})
}
# Run the application
shinyApp(ui = ui, server = server)
图书馆(tidyverse)
#为绘制直方图的应用程序定义UI
哇,这看起来很棒。虽然当我尝试运行时,我得到了错误:parse中的错误(file,keep.source=FALSE,srcfile=src,encoding=encc):/Desktop/app.R:83:5:unexpected'else'82:}83:else^ readLines(path)中的警告:“/Desktop/app.R”上的最后一行不完整可能缺少逗号:61:for(2:500中的I){^可能在:61:for(2:500中的i)处缺少逗号{^…抱歉,我在循环中犯了一个错误。现在它应该可以工作:-)。无论如何,它仍然不完美,因为当计数器等于nrow(df)时,失效应该停止。
library(shiny)
library(tidyverse)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Simple Testcase"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
textInput("textinput", "Type text here"),
numericInput("p1", "Probability1", value = 0.5),
numericInput("p2", "Probability2", value = 0.9),
sliderInput("iteration", "Iterations", min = 20, max = 1000, step = 10, value = 100),
actionButton("calc", "Run Calculation!")
),
# Show a plot of the generated distribution
mainPanel(
tableOutput("ui")
)
)
)
# Define server logic required to draw a histogram
server <- function(session ,input, output) {
vals <- reactiveValues(counter = 0)
result <- eventReactive(input$calc, {
evolve_sentence <- function(sentence, arg2) {
chars <- str_split(sentence, "") %>% pluck(1)
if (runif(1) > input$p1) { # Value from numericinput p2
chars[sample(1:length(chars), 1)] <- sample(chars, 1)
}
sentence <- str_c(chars, collapse = "")
words <- str_split(sentence, " ") %>% pluck(1)
if (runif(1) > input$p2) { # Value from numericinput p2
words[sample(1:length(words), 1)] <- sample(words, 1)
}
sentence <- str_c(words, collapse = " ")
sentence
}
tbl_evolve <- tibble(iteration = 1:500, text = input$textinput)
for (i in 2:500) {
tbl_evolve$text[i] <- evolve_sentence(tbl_evolve$text[i - 1])
}
output <-tbl_evolve %>%
distinct(text, .keep_all = TRUE)
print(output)
output
})
output$ui <- renderTable({
df <- result()
invalidateLater(millis = 300, session)
vals$counter <- isolate(vals$counter) + 1
while(nrow(df) < vals$counter) {
vals$counter <- isolate(vals$counter) + 1
} #Prevent to add infinite empty columns.
for(i in 1:nrow(df)) {
newdf <- df[1:vals$counter,]
}
newdf
})
}
# Run the application
shinyApp(ui = ui, server = server)